package com.example.pkmedical.Fragments.rollover;


import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.pkmedical.Base.BaseFragment;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RolloverCustomizedFragment extends BaseFragment {

    @BindView(R.id.customized_right_minute_num)
    TextView customized_right_minute_num;
    @BindView(R.id.customized_left_num_layout_num)
    TextView customized_left_num_layout_num;
    @BindView(R.id.customized_left_num_layout)
    RelativeLayout customizedLeftNumLayout;
    @BindView(R.id.customized_left_minute)
    RelativeLayout customizedLeftMinute;
    @BindView(R.id.customized_center_minute_num)
    TextView rolloverCenterText;
    @BindView(R.id.rollover_center_button_text)
    TextView rolloverCenterButtonText;
    @BindView(R.id.customized_left_minute_text)
    TextView customized_left_minute_text;
    @BindView(R.id.rollover_center_button)
    RelativeLayout rolloverCenterButton;
    @BindView(R.id.customized_right_num_layout)
    RelativeLayout customizedRightNumLayout;
    @BindView(R.id.customized_center_minute)
    RelativeLayout customizedCenterMinute;
    @BindView(R.id.customized_right_num_layout_num)
    TextView customizedRightNumLayoutNum;
    @BindView(R.id.customized_right_minute)
    RelativeLayout customizedRightMinute;
    private PopupWindow bfbPop, timePop;

    private PopupWindow YesNoPop;
    private boolean nowType = false;
    //左翻百分比/左翻时间

    /**
     * 动作指令组合
     */
    @Override
    public int setLayoutId() {
        return R.layout.fragment_rollover_customized;
    }

    @Override
    public void findViewById(View view) {

    }

    @Override
    public void setViewData(View view) {
        customized_right_minute_num.setText(SPUtils.getInstance().getInt("rollover_left_minute") + "分钟");
        customizedRightNumLayoutNum.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
        rolloverCenterText.setText(SPUtils.getInstance().getInt("rollover_ping_minute") + "分钟");
        customized_left_minute_text.setText(SPUtils.getInstance().getInt("rollover_right_minute") + "分钟");
        customized_left_num_layout_num.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");

    }

    @Override
    public void setClickEvent(View view) {

    }

    private int bfbNow;
    private int now;

    /**
     * 时间选择
     *
     * @param button
     * @param o
     */
    private void initTimeWindow(final TextView button, final int o) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_time, null);
        final TextView tv_it_time = view.findViewById(R.id.tv_it_time);
        ImageView iv_it_left = view.findViewById(R.id.iv_it_left);
        ImageView iv_it_right = view.findViewById(R.id.iv_it_right);
        final SeekBar sb_it = view.findViewById(R.id.sb_it);
        String btStr = button.getText().toString();
        now = Integer.parseInt(btStr.substring(0, btStr.indexOf(getString(R.string.minute))));
        tv_it_time.setText(now + "");
        sb_it.setProgress(now);


        iv_it_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (now > 3) {
                    now = now - 1;
                    tv_it_time.setText(now + "");
                    button.setText(now + getString(R.string.minute));
                    sb_it.setProgress(now);
                    switch (o) {
                        case 1:
                            SPUtils.getInstance().put("rollover_left_minute", now);
                            break;
                        case 2:
                            SPUtils.getInstance().put("rollover_ping_minute", now);
                            break;
                        case 3:
                            SPUtils.getInstance().put("rollover_right_minute", now);
                            break;
                    }
                }
            }
        });
        iv_it_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (now < 30) {
                    now = now + 1;
                    tv_it_time.setText(now + "");
                    button.setText(now + getString(R.string.minute));
                    sb_it.setProgress(now);
                    switch (o) {
                        case 1:
                            SPUtils.getInstance().put("rollover_left_minute", now);
                            break;
                        case 2:
                            SPUtils.getInstance().put("rollover_ping_minute", now);
                            break;
                        case 3:
                            SPUtils.getInstance().put("rollover_right_minute", now);
                            break;
                    }
                }
            }
        });
        sb_it.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 3) {
                    now = i / 1;
                    button.setText(now + getString(R.string.minute));
                    tv_it_time.setText(now + "");
                } else{
                    seekBar.setProgress(3);
                    now = 3;
                    button.setText(now + getString(R.string.minute));
                    tv_it_time.setText(now + "");
                }
                switch (o) {
                    case 1:
                        SPUtils.getInstance().put("rollover_left_minute", now);
                        break;
                    case 2:
                        SPUtils.getInstance().put("rollover_ping_minute", now);
                        break;
                    case 3:
                        SPUtils.getInstance().put("rollover_right_minute", now);
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        timePop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        timePop.setFocusable(true);
        timePop.setOutsideTouchable(true);
        timePop.setBackgroundDrawable(new ColorDrawable());
        int xoff = (timePop.getContentView().getMeasuredWidth() - button.getWidth()) / 2;
        timePop.showAsDropDown(button, -xoff, 0);
    }

    /**
     * %选择
     *
     * @param button
     */
    private void initNumWindow(final TextView button, int xzType) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_time, null);
        final TextView tv_it_time = view.findViewById(R.id.tv_it_time);
        ImageView iv_it_left = view.findViewById(R.id.iv_it_left);
        ImageView iv_it_right = view.findViewById(R.id.iv_it_right);
        TextView tv_it_mtime = view.findViewById(R.id.tv_it_mtime);
        final SeekBar sb_it = view.findViewById(R.id.sb_it);
        sb_it.setMax(10);
        tv_it_mtime.setText("100");
        String btStr = button.getText().toString();
        bfbNow = Integer.parseInt(btStr.substring(0, btStr.indexOf("%")));
        tv_it_time.setText(bfbNow + "");
        sb_it.setProgress(bfbNow / 10);
        iv_it_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bfbNow > 10) {
                    bfbNow = bfbNow - 10;
                    sb_it.setProgress(bfbNow / 10);
                }
                switch (xzType) {
                    case 0:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;

                    case 2:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;
                }
            }
        });
        iv_it_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bfbNow < 100) {
                    bfbNow = bfbNow + 10;
                    sb_it.setProgress(bfbNow / 10);
                }
                switch (xzType) {
                    case 0:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;

                    case 2:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;
                }
            }
        });
        sb_it.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 1) {
                    bfbNow = i * 10;
                    button.setText(bfbNow + "%");
                    tv_it_time.setText(bfbNow + "");
                } else {
                    seekBar.setProgress(1);
                    bfbNow = 10;
                    button.setText(bfbNow + "%");
                    tv_it_time.setText(bfbNow + "");
                }
                switch (xzType) {
                    case 1:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;
                    case 3:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
//                        list.get(0).setLevel(bfbNow / 10 - 1);
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        bfbPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        bfbPop.setFocusable(true);
        bfbPop.setOutsideTouchable(true);
        bfbPop.setBackgroundDrawable(new ColorDrawable());
        int xoff = (bfbPop.getContentView().getMeasuredWidth() - button.getWidth()) / 2;
        bfbPop.showAsDropDown(button, -xoff, 0);
    }

    @OnClick({R.id.customized_right_minute, R.id.customized_center_minute, R.id.customized_left_num_layout, R.id.customized_left_minute, R.id.rollover_center_button, R.id.customized_right_num_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.customized_left_num_layout:
                //左侧卧%选择
                initNumWindow(customized_left_num_layout_num, 1);
                break;
            case R.id.customized_left_minute:
                //左侧卧分钟选择
                initTimeWindow(customized_left_minute_text, 3);
                break;
            case R.id.rollover_center_button:
                initYesNoWindow();
                break;
            case R.id.customized_right_num_layout:
                //右侧卧%选择
                initNumWindow(customizedRightNumLayoutNum, 3);
                break;
            case R.id.customized_right_minute:
                //右侧卧分钟选择
                initTimeWindow(customized_right_minute_num, 1);
                break;
            case R.id.customized_center_minute:
                //平躺分钟选择
                initTimeWindow(rolloverCenterText, 2);
                break;
        }
    }

    /**
     * 是否选择器
     */
    private void initYesNoWindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_yes_no_layout, null);
        RelativeLayout tv_yes = view.findViewById(R.id.rollover_item_yes);
        RelativeLayout tv_no = view.findViewById(R.id.rollover_item_no);
        if (nowType) {
            tv_yes.setVisibility(View.GONE);
        } else {
            tv_no.setVisibility(View.GONE);
        }

        tv_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rolloverCenterButtonText.setText(R.string.rollover_select_yes);
                nowType = true;
                YesNoPop.dismiss();
            }
        });
        tv_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rolloverCenterButtonText.setText(R.string.rollover_select_no);
                nowType = false;
                YesNoPop.dismiss();
            }
        });
        YesNoPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        YesNoPop.setFocusable(true);
        YesNoPop.setOutsideTouchable(true);
        YesNoPop.setBackgroundDrawable(new ColorDrawable());
        YesNoPop.showAsDropDown(rolloverCenterButton);

    }
}
